<?php
/**
 * SdataService
 */
namespace App\Service;

use Phalcon\Di;
use App\Models\Data;
use App\Models\EwData;

class SdataService  extends BaseService
{
	const MIGRATE_TRUE = 1;		//入库标识：是
	const MIGRATE_FALSE = 0;	//入库标识：否
	const DEL_TRUE = 1;			//删除标识：是
	const DEL_FALSE = 0;		//删除标识：否

	/**
	 * [getList 列表 APP端列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['d.id,d.title,d.thumb,d.source,d.content,d.url']);
		$builder->addFrom("App\Models\Data", 'd');
		$builder->andWhere("d.is_del = " . self::DEL_FALSE);

		//is_migrate 
		if (isset($params->is_migrate))
			$builder->andWhere("d.is_migrate = ". $params->is_migrate);

		//title
		if (isset($params->title) && $params->title)
			$builder->andwhere("d.title LIKE '%" . $params->title . "%'");

		//content
		if (isset($params->content) && $params->content)
			$builder->andwhere("d.content LIKE '%" . $params->content . "%'");

		//report_source
		if (isset($params->report_source) && $params->report_source)
			$builder->andwhere("d.report_source LIKE '%" . $params->report_source . "%'");

		//source
		if (isset($params->source) && $params->source)
			$builder->andwhere("d.source LIKE '%" . $params->source . "%'");

		//sources
		if (isset($params->sources) && $params->sources)
			$builder->andwhere("d.source LIKE '%" . $params->sources . "%'");
		
		//count 数量
		if ($count)
			return $builder->getQuery()->execute()->count();

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by, $table = "d");
		}else{
			$sql = " d.id DESC";
		}
		$builder->orderBy($sql);

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();	
	}
	
	/**
	 * [getInfo 详情]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function getInfo($id)
	{
		return Data::findFirst($id);
	}

	/**
	 * [delete 移除/恢复数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function delete($id)
	{
		if (isset($id) && $id)
			$data = Data::findFirst($id);

		if ($data){
			$data->is_del = $data->is_del ? self::DEL_FALSE : self::DEL_TRUE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [forever 彻底删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = Data::findFirst($id);

		if ($row && $row->delete())
			return TRUE;

		return NULL;
	}

	/**
	 * [migrate 入库]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function migrate($id)
	{
		$info = Data::findFirst($id);
		if (!$info || $info->is_migrate == self::MIGRATE_TRUE)
			return NULL;

		foreach ($info as $key => $value) {
			$info->$key = preg_replace("/\\n/", "", $value);
			$info->$key = trim($info->$key);
		}

		$pics_arr = explode("::", $info->content);
		$pics = implode(',', $pics_arr);

		$data = new \stdClass();
		$data->id = $id;
		$data->command = "SDATA";
		$data->city_id = 63;
		$data->title = $info->title;

		//2017-3-29 +
		$data->is_self = 0;
		$data->three_url = $info->url;

		$data->short_title = $info->tags;
		$data->publish_time = $info->publish_time ? $info->publish_time : date("Y-m-d H:i:s", time());
		// $data->tags = "";
		$data->summary = $info->outline;
		// $data->author = "";
		$data->cate_id = 9;
		$data->type_id = 1;
		$data->thumb = $info->thumb;
		$data->pics = $pics;
		$data->price = 0;
		// $data->content = "";
		$data->sources = $info->source;
		// $data->doc = "";
		// $data->download = 0;
		// $data->share = 0;
		// $data->visit = 0;
		// $data->comment = 0;
		// $data->is_free = 1;
		// $data->is_display = 1;
		// $data->is_del = 0;
		// $data->order_by = 100;
		// $data->create_time = date("Y-m-d H:i:s", time());
		// $data->update_time = date("Y-m-d H:i:s", time());
		
		$redis = Di::getDefault()['redis'];
		$queue = Di::getDefault()['queue'];

		$redis->set("SDATA_{$info->id}", $info->id);
		$redis->expireAt("SDATA_{$info->id}", time() + 60*5);
		$queue->put(json_encode((array)$data));

		// $row = new EwData;

		// if (!$row->save((array)$data))
		// {
		// 	$msg =$row->getMessages();
		// 	var_dump($msg);die;
		// }
		// if ($row->save((array)$data))
		// {
		// 	$info->is_migrate = self::MIGRATE_TRUE;
		// 	$info->save();
		// }
	}

	/**
	 * [batchMigrate 批量入库]
	 * @param  [type] $id_arr [description]
	 * @return [type]         [description]
	 */
	public static function batchMigrate($id_arr = [])
	{
		if ($id_arr && is_array($id_arr) && count($id_arr) > 0){
			foreach ($id_arr as $id) {
				self::migrate($id);
			}
		}
	}
}